package com.swa99ylum.pjv.corrector;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Map;

import com.swa99ylum.pjv.corrector.Corrector;

/** <h1>日期格式 修正器</h1>
<br> 1、将 value 的 日期格式 修正为 新的日期格式
<br> 2、pattern 的用法和 SimpleDateFormat 完全一样
<br>    
 */
public class DateFormatCorrector extends Corrector {
	
	private String oldPattern;
	private String newPattern;
	
	
	public DateFormatCorrector(String oldPattern, String newPattern) {
		super();
		this.oldPattern = oldPattern;
		this.newPattern = newPattern;
	}


	@Override
	public void doCorrect(Object rootObject, String[] nodePath, String nodeName, Map<String, Object> parentValue, Object incorrectValue) {
		try {
			DateFormat df1 = new SimpleDateFormat(oldPattern);
			DateFormat df2 = new SimpleDateFormat(newPattern);
			
			String value = df2.format(df1.parse(String.valueOf(incorrectValue)));			
			
//			JSONPath.set(rootObject, nodePath, value);
			parentValue.put(nodeName, value);
			
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}
}
